$(document).ready(function(){
	cannotEnter();
	getVerifyCode();//加载页面，初始化图片验证码
	blurAccountCheck();
	banInputSpace();
	clickToGetNewCode();
	registerValidate();
	register();
});

function register(){
	$("#submitBtn").click(function(){
		if(isRegisterValidate.checkForm()){
			var url = "/HyperIT/user/sendEmail";
			var parm = $("#registerForm").serialize();
			$.get(url,parm,function(data){
				if(data.serviceResult){
					alert("邮件已发送到您的邮箱，请半小时内前往激活。");
				}else{
//					$("#verifyCode").parent().append('<label for="verifyCode">'+data.resultInfo+'</label>');
					alert(data.resultInfo);
				}
			});
		}else{
			isRegisterValidate.showErrors();
		}
	});
}
//屏蔽空格输入
function banInputSpace(){
	$("input").focus(function(){
		$(document).keydown(function(e){
			var keycode;
			if(window.event){
				keycode = e.keyCode;
			}else if(e.which){
				keycode = e.which;
			}if(keycode==32){
				return false;
			}
			return true;
		});
	});
}
//获取图片验证码
function getVerifyCode(){
	var url = "/HyperIT/user/getImgVerifyCode";
	$.get(url, function(data){
		if(data.serviceResult){
			$("#verifyCodeImg img").attr("src","../../"+data.resultParm.path);
		}else{
			return false;
		}
	});
}
//点击验证码，获取新的验证码
function clickToGetNewCode(){
	$("#verifyCodeImg img").click(function(){
		console.log("点击获取新的图片验证码.");
		getVerifyCode();
	});
}
//账号input失焦检测，该账号是否已经被注册
function blurAccountCheck(){
	$("#account").blur(function(){
		var url = "/HyperIT/user/isRegister";
		var data = "account="+$("#account").val();
		$.get(url,data,function(data){
			if(data.serviceResult){
				if(!data.resultParm.flag){
					$("#account").parent().children("label").eq(1).remove();
					$("#account").parent().append('<label for="account">'+data.resultInfo+'</label>');
				}else{
					$("#account").parent().children("label").eq(1).remove();
				}
			}else{
				return false;
			}
		});
	});
}
//验证码input失焦检测，判断验证码是否正确
function checkVerifyCode(){
	
}
//注册表单验证
function registerValidate(){
	isRegisterValidate = $("#registerForm").validate({
		onsubmit: false,
		onkeyup: false, 
		onfocusout: function(element){
			$(element).valid();
	  	},
   		errorPlacement: function(error, element) {  
   			error.appendTo(element.parent());
   		},
		rules:{
			account:{
				required:true,
				email:true
			},
			password:{
				required:true,
				minlength:6,
				maxlength:16
			},
			verifyCode:{
				required:true
			}
		},
		messages:{
			account:{
				required:"请输入账号！",
				email:"请输入邮箱测正确格式！"
			},
			password:{
				required:"请输入密码！",
				minlength:"密码不能小于6个字符！",
				maxlength:"密码不能多于16个字符！"
			},
			verifyCode:{
				required:"请输入验证码！"
			}
		}
	});
}
